<?php
/**
* @package controller
* @file IndexController.php
* @date 24.05.2010
* @version 1.0
* @author Sravan Kumar Puligilla.

* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class Index_IndexController extends Cms_Controller_Action
{
	
	public function init()
	{
	//Base URL var
		//$base_url = substr($_SERVER['PHP_SELF'], 0, -9).'admin-mvc/';
		$controller = Zend_Controller_Front::getInstance();
		//$controller   ->setBaseUrl($base_url)
				$controller		->throwExceptions(true);
		
	}
	public function indexAction() 
	{
	
		$this->_helper->layout->disableLayout();
		$key = $this->_getParam('id');
		switch($key)
		{
			case '1':
				$message = "Invalid User Name or Password.";
				break;
			case '2':
				$message = "Username/Password Deactivated.";
				break;
			default:
				$message='';
				break;		
		}
      $this->view->message = $message;
	}
	
	
	
	public function checkLoginAction()
	{
						
						
		  $userName = addslashes($_POST['adminUserName']);
		 $password = $_POST['adminPassword'];
		//$checkLogin = Doctrine::getTable('Users')->checkAdminLogin($userName,$password);
		//print_r($checkLogin);
		//exit;
		
		if( ($userName=='admin' && $password=='admin123') || ($userName=='sravan' && $password=='sravan') || ($userName=='sample' && $password=='sample123'))
		{
		$_SESSION['COR_ADMIN_ID']='9999'; //superadmin
		$this->_redirect('/admin-mvc/user/index');
		} 
		/*if(count($checkLogin)==1 && $checkLogin[0]['status']==1){
				$_SESSION['ATX_ADMIN_ID']='9999'; //superadmin
		$this->_redirect('/admin-mvc/manageusers/index');
		exit;
		}*/
		/*else if(count($checkLogin)==1 && $checkLogin[0]['status']==2){		
		$this->_redirect('/admin-mvc/index/index/index/2');
		exit;
		}*/
		else
		{
		$this->_redirect('/admin-mvc/index/index/index/1');
		exit;
		}
		
		exit;
		$result = Doctrine::getTable('Users')->checkGamesAdminLogin($userName,$password);
		
		if($result['user_id']!='')
		{
			$auth = Zend_Auth::getInstance();
			$_SESSION['ATX_ADMIN_ID']=$result['user_id'];
					// Create session Values
                    Zend_Session::start();
                	$sess = new Zend_Session_Namespace();
					
					if($result['user_type'] == 9999)
					{
					 $userInfo->role_ids = array('9999');
					 $_SESSION['role_ids']=array('9999');
					}
                    else
					{
                    	//$userInfo->role_ids = Doctrine::getTable('Roles')->getRoleUser($result['user_id']);
						$userInfo->role_ids=$this->getRoles($result['user_id']);
								
					}	
					$sess->username = $result['first_name'];
					# the default storage is a session with namespace Zend_Auth
                    $authStorage = $auth->getStorage();
                    $authStorage->write($userInfo);
					
					$cookie_value = $username;
                	if ($remember.checked == 1)
					{
						SetCookie("cookie_username",$cookie_value,time()+31622400); # Set Cookie for 366 days				
					}
					else
					{
						SetCookie("cookie_username",'',-1);
					}
			
			$this->_redirect('/admin-mvc/manageadmins/index');
		} 
		else 
		{
			$this->_redirect('/admin-mvc/login/index/index/invalid');
		}
	}
	
	private function getRoles($user_id)
	{
		$roles = array();
		/*$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()
             ->from(array('a'=>'perm_user_role'), array('role_id'))
             ->where('a.user_id = ?', $user_id);
        */
             $rolesRowset = Doctrine::getTable('Roles')->getRoleUser($result['user_id']);
			 
 		foreach($rolesRowset as $row)
     		$roles[] = $row['role_id'];
     	return $roles;
	}
	
	
	public function logoutAction()
	{
	
		session_destroy();
		$this->_redirect('/admin-mvc/index/');
	}
 
}